package com.example.jwtjava.controller;

import com.example.jwtjava.pojo.User;
import com.example.jwtjava.common.R;
import com.example.jwtjava.util.JwtUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author 宋伟宁
 * @Date 2023/10/23
 * @Version 1.0
 **/
@RestController
public class LoginController {

    @GetMapping("/hello")
    public R test(){
        return new R(200,true,"测试成功");
    }

    @GetMapping("/checkToken")
     public R checkToken(String token){
          return  JwtUtils.checkToken(token)
                  ? new R(200,true,"验证通过")
                  : new R(401,false,"验证失败");
     }
      @PostMapping("/login")
      public R login(@RequestBody User loginUser){
          if(loginUser.getUsername().equalsIgnoreCase("admin")
                  && loginUser.getPassword().equals("1234")){
              //生成token并保存到用户对象中
              //生成token
              loginUser.setToken(JwtUtils.createToken());
              return  new R(200,true,"登录成功",loginUser);
          }
          return new R(301,false,"登录失败");
      }

      @GetMapping("/createToken")
      public R testLogin(){
           return new R(200,true,"OK", JwtUtils.createToken());
      }
}
